using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using UCS.XNA.Draughts.Boards;
using UCS.XNA.Draughts.Pieces;
using UCS.XNA.Utils;

namespace UCS.XNA.Draughts.Squares
{
    public class Square
    {
        #region Attributes and Properties

        private SquareType _type;
        public SquareType Type
        {
            get { return this._type; }
            set { this._type = value; }
        }

        private Piece _piece;
        public Piece Piece
        {
            get { return this._piece; }
            set
            {
                this._piece = value;

                if (this._piece != null)
                    this._piece.BoardPosition = this.BoardPosition;
            }
        }

        private BoardPosition _boardPosition;
        public BoardPosition BoardPosition
        {
            get { return this._boardPosition; }
            set { this._boardPosition = value; }
        }

        private SquareState _state;
        public SquareState State
        {
            get { return _state; }
            set { _state = value; }
        }

        public bool IsEmpty
        {
            get { return _piece == null; }
        }

        #endregion

        #region Private Members

        private Texture2D _whiteSquareTexture;
        private Texture2D _blackSquareTexture;
        private Texture2D _validTexture;
        private Texture2D _invalidTexture;

        #endregion

        #region Constructors

        public Square(SquareType type, BoardPosition boardPosition)
        {
            this._state = SquareState.Normal;
            this._type = type;
            this._boardPosition = boardPosition;
        }

        #endregion

        #region Private Methods

        private void DrawPiece(GameTime gameTime)
        {
            if (this._piece != null)
                this._piece.Draw(gameTime);
        }

        #endregion

        #region Public Methods

        public void InitializeTextures()
        {
            _whiteSquareTexture = GameResources.Content.Load<Texture2D>("WhiteSquare");
            _blackSquareTexture = GameResources.Content.Load<Texture2D>("BlackSquare");
            _validTexture = GameResources.Content.Load<Texture2D>("GreenSquare");
            _invalidTexture = GameResources.Content.Load<Texture2D>("RedSquare");
        }

        public Square Clone()
        {
            Square cloned = new Square(_type, _boardPosition.Clone());
            if (_piece != null)
                cloned.Piece = _piece.Clone();

            return cloned;
        }

        public void Update(GameTime gameTime)
        {
            if (this._piece != null)
                this._piece.Update(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            int x = this._boardPosition.J * 58 + 2;
            int y = this._boardPosition.I * 58 + 2;

            GameResources.SpriteBatch.Draw(this._type == SquareType.White ? this._whiteSquareTexture : this._blackSquareTexture, new Vector2(x, y), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);

            if (_state == SquareState.Valid)
                GameResources.SpriteBatch.Draw(this._validTexture, new Vector2(x, y), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.4f);
            else if (_state == SquareState.Invalid)
                GameResources.SpriteBatch.Draw(this._invalidTexture, new Vector2(x, y), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0.4f);

            this.DrawPiece(gameTime);
        }

        #endregion
    }
}
